package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.ComVo;
import tech.aistar.entity.Point;
import tech.aistar.entity.Post;
import tech.aistar.entity.PostVo;
import tech.aistar.service.*;
import tech.aistar.util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/10/8.
 */
@Controller
@RequestMapping("/com")
public class ComAction {
    @Autowired
    private IComService comService;
    @Autowired
    private IComVoService comVoService;
    @Autowired
    private IPostService postService;
    @Autowired
    private IPointService pointService;
    /**
     * 雪花id生成器
     */
    @Autowired
    private IdWorker idWorker;

    @RequestMapping("/comment")
    public String comments(Model model,Integer postId){
//        //展示帖子的一级评论
//        List<Com> comList = comService.findOneComment(1);//postId,flag
//        model.addAttribute("comList",comList);
//        //二级评论
//        List<Com> comList1 = comService.findAllTwoComment(2,1);//flag,postId
//        model.addAttribute("comList1",comList1);
        //显示所有评论
        /**
         * 进入对应帖子
         */
        PostVo postOne = postService.findPostByPostId1(postId);
        model.addAttribute("postOne",postOne);

        //帖子点赞
//        Integer pointCount = pointService.findStarLikesCount(postId);
//        model.addAttribute("pointCount",pointCount);

        /**
         * 展示该帖子所有评论
         */
        List<ComVo> comVoList = comVoService.findAllByPostId(postId);
        System.err.println(comVoList);
        //二级评论数
//        Long comCount = comService.findTwoCount()
        model.addAttribute("comVoList",comVoList);

        return "forum/post";
    }

    //点赞
//    @RequestMapping("/point")
//    public String point(Model model, HttpServletRequest request){
//        Integer postId = Integer.valueOf(request.getParameter("postId"));
//
//        //防止用户重复点赞，进行判断
//        if (pointService.findPointByUidAndStraId(1,postId)==null){
//            Point point = new Point();
//            point.setUserId(1);
//            point.setType(1);
//            point.setPostId(postId);
//            point.setStatus(0);
//            point.setTime(new Date());
//            point.setDelTime(null);
//            pointService.addPoint(point);
//        }else {
//            if(pointService.findPointByUidAndStraId(1,postId).getStatus()==1){
//                pointService.updatestatusTo0(1,postId);
//            }else {
//                pointService.updatestatusTo1(1,postId);
//            }
//        }
//        Integer pointCount = pointService.findStarLikesCount(postId);
//        return String.valueOf(pointCount);
//    }

    //收藏
    @RequestMapping("/collection")
    @ResponseBody
    public String collection(Integer uid,Integer kindId,HttpServletRequest request){

        if (request.getSession().getAttribute("user")==null){
            return "未登录";
        }else{
            pointService.addPosts(uid,kindId);
            return "可收藏";
        }

    }

}
